<!--
Copyright 2013 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html><meta charset="UTF-8">
<script src="../bootstrap.js"></script>
<script>
"use strict";

test(function() {
  var element = document.createElement('span');
  assert_styles(element, {},
      'assert_styles should support an element with no parent');

  document.body.appendChild(element);
  var childCount = document.body.children.length;
  assert_styles(element, {});
  assert_equals(childCount, document.body.children.length,
      'assert_styles should not leave additional elements in the dom');
}, 'assert_styles reference_element');

test(function() {
  try {
    assert_styles(null, {});
    assert_unreached();
  } catch (e) {
    assert_equals(e.message, 'Expected Array, NodeList or Element but got null');
  }
}, 'assert_styles invalid arguments');

test(function() {
  var element = document.createElement('span');
  document.body.appendChild(element);

  try {
    assert_styles(element, {'left': 'abc123'}, "Description 1");
    assert_unreached();
  } catch (e) {
    assert_regexp_match(e.message, /^Tried to set the reference element\'s left to "abc123" but neither/);
  }

  try {
    assert_styles(element, {'clip': 'rect(1px 2px 3px 4px)'});
    assert_unreached();
  } catch (e) {
    assert_regexp_match(e.message, /Actual - /);
  }
}, 'assert_styles given invalid style');

test(function() {
  var element = document.createElement('span');
  document.body.appendChild(element);
  element.style.left = '-100px';
  try {
    assert_styles(element, {left: '100px'});
    assert_unreached();
  } catch(e) {
  }
}, 'assert_styles should handle negative specified values');

test(function() {
  var element = document.createElement('span');
  document.body.appendChild(element);
  element.style.left = '100px';
  try {
    assert_styles(element, {left: '-100px'});
    assert_unreached();
  } catch(e) {
  }
}, 'assert_styles should handle negative expected values');

test(function() {
  var element = document.createElement('span');
  document.body.appendChild(element);
  element.style.left = '-100px';
  assert_styles(element, {left: '-100px'});
}, 'assert_styles should handle negative values');

test(function() {
  var AssertionError = window.assert_styles_assertion_error;
  var assert = window.assert_styles_assert_important_in_array;

  assert('matrix(1%)', ['matrix(1%)']);
  assert('matrix(1%)', ['', 'matrix(1%)']);
  assert('matrix(1% 1px)', ['matrix(1%, 1px)']);
  assert('rect(1px 2px 3px 4px)', ['rect(1px 2px 3px 4px)']);
  assert('rect(1px 2px 3px 4px)', ['rect(1px, 2px, 3px, 4px)']);

  try {
    assert('', [], 'empty');
    assert_unreached();
  } catch(e) {
    assert_regexp_match(e.message, /^empty/);
  }

  try {
    assert('matrix(1%)', [], 'matrix 1% to empty');
    assert_unreached();
  } catch(e) {
    assert_regexp_match(e.message, /^matrix 1% to empty/);
  }

  try {
    assert('matrix(1%)', [''], 'matrix 1% to empty string');
    assert_unreached();
  } catch(e) {
    assert_regexp_match(e.message, /^matrix 1% to empty string\n/);
  }

  try {
    assert('matrix(1%)', ['matrix(1px)'], 'matrix 1% to matrix 1px');
    assert_unreached();
  } catch(e) {
    assert_regexp_match(e.message, /^matrix 1% to matrix 1px/);
  }

  /* Check that it only needs to match to 4 significant figures */
  assert('1', [1.0001]);
  assert('1.0001', [1]);
  try {
    assert('1', [1.001]);
    assert_unreached();
  } catch(e) {
    assert_not_equals(e.message, "assert_unreached: Reached unreachable code");
  }

  assert('10', [10.001]);
  assert('10.001', [10]);
  try {
    assert('10', [10.01]);
    assert_unreached();
  } catch(e) {
    assert_not_equals(e.message, "assert_unreached: Reached unreachable code");
  }

  assert('2.0', [2.0001]);
  assert('2.0001', [2]);
  try {
    assert('2', [2.001]);
    assert_unreached();
  } catch(e) {
    assert_not_equals(e.message, "assert_unreached: Reached unreachable code");
  }

  assert('20.0', [20.001]);
  assert('20.001', [20]);
  try {
    assert('20.0', [20.01]);
    assert_unreached();
  } catch(e) {
    assert_not_equals(e.message, "assert_unreached: Reached unreachable code");
  }

  assert('40e2', [4000.1]);
  assert('40.001e2', [4000]);
  try {
    assert('40e2', [4001]);
    assert_unreached();
  } catch(e) {
    assert_not_equals(e.message, "assert_unreached: Reached unreachable code");
  }

  assert('40e+3', [40001]);
  assert('40.001e+3', [40000]);
  try {
    assert('40e+3', [40010]);
    assert_unreached();
  } catch(e) {
    assert_not_equals(e.message, "assert_unreached: Reached unreachable code");
  }

  assert('50e-4', [0.0050001]);
  assert('50.001e-4', [0.005]);
  try {
    assert('50e-4', [0.005001]);
    assert_unreached();
  } catch(e) {
    assert_not_equals(e.message, "assert_unreached: Reached unreachable code");
  }

}, 'Checking assert_important_in_array works');

test(function() {
  var ei = window.assert_styles_extract_important;

  assert_array_equals(ei('1'), [1]);
  assert_array_equals(ei('10'), [10]);
  assert_array_equals(ei('2.0'), [2.0]);
  assert_array_equals(ei('20.0'), [20.0]);
  assert_array_equals(ei('40e2'), [4000.0]);
  assert_array_equals(ei('40e+3'), [40000.0]);
  assert_array_equals(ei('50e-4'), [0.005]);
  assert_array_equals(ei('60.6e-4'), [0.00606]);

  assert_array_equals(ei('+1'), [1]);
  assert_array_equals(ei('+10'), [10]);
  assert_array_equals(ei('+2.0'), [2.0]);
  assert_array_equals(ei('+20.0'), [20.0]);
  assert_array_equals(ei('+40e2'), [4000.0]);
  assert_array_equals(ei('+40e+3'), [40000.0]);
  assert_array_equals(ei('+50e-4'), [0.005]);
  assert_array_equals(ei('+60.6e-4'), [0.00606]);

  assert_array_equals(ei('-1'), [-1]);
  assert_array_equals(ei('-10'), [-10]);
  assert_array_equals(ei('-2.0'), [-2.0]);
  assert_array_equals(ei('-20.0'), [-20.0]);
  assert_array_equals(ei('-40e2'), [-4000]);
  assert_array_equals(ei('-50e-3'), [-0.05]);
  assert_array_equals(ei('-60.6e-4'), [-0.00606]);

  assert_array_equals(ei('matrix(1%'), ['matrix', 1, '%']);
  assert_array_equals(ei('matrix(10px'), ['matrix', 10, 'px']);
  assert_array_equals(ei('matrix(2.0em'), ['matrix', 2.0, 'em']);
  assert_array_equals(ei('matrix(20.0en'), ['matrix', 20.0, 'en']);
  assert_array_equals(ei('matrix(40e2ve'), ['matrix', 4000.0, 've']);
  assert_array_equals(ei('matrix(40e+3vw'), ['matrix', 40000.0, 'vw']);
  assert_array_equals(ei('matrix(50e-4%'), ['matrix', 0.005, '%']);

  assert_array_equals(ei('matrix('), ['matrix']);
  assert_array_equals(ei('blah'), ['blah']);
  assert_array_equals(ei('+'), []);
  assert_array_equals(ei('none'), ['none']);
  assert_array_equals(ei('----'), []);
  assert_array_equals(ei('-a'), ['a']);
  assert_array_equals(ei('b.j'), ['b', 'j']);

  assert_array_equals(ei('matrix(1, 0, 0, 1, 0, 0)'),
      ['matrix', 1, 0, 0, 1, 0, 0]);
  assert_array_equals(ei('matrix(1 0 0 1 0 0)'),
      ['matrix', 1, 0, 0, 1, 0, 0]);
  assert_array_equals(ei('matrix(0.9511, 0.309, -0.309, 0.9511, 0, 0)'),
      ['matrix', 0.9511, 0.309, -0.309, 0.9511, 0, 0]);

}, 'Checking important parts are extracted successfully');

</script>
